package pikes.ecma;

import java.io.IOException;

public class IfStatement implements Statement {

	private IExpression expression = null;

	private Statement trueStatement = null;

	private Statement elseStatement = null;

	public IfStatement(IExpression expression, Statement trueStatement) {
		this.expression = expression;
		this.trueStatement = trueStatement;
	}

	public IfStatement(IExpression expression, Statement trueStatement, Statement elseStatement) {
		this(expression, trueStatement);
		this.elseStatement = elseStatement;
	}

	public void print(Appendable appendable) throws IOException {
		appendable.append("if(");
		expression.print(appendable);
		appendable.append(")");
		trueStatement.print(appendable);
		if (elseStatement != null) {
			appendable.append("else");
			elseStatement.print(appendable);
		}

	}

	public void visit(SyntaxTreeVisitor syntaxTreeVisitor) throws EcmaSyntaxVisitingException {
		syntaxTreeVisitor.If();
		syntaxTreeVisitor.beforeParameterList();
		expression.visit(syntaxTreeVisitor);
		syntaxTreeVisitor.afterParameterList();

		trueStatement.visit(syntaxTreeVisitor);
		if (elseStatement != null) {
			syntaxTreeVisitor.Else();
			elseStatement.visit(syntaxTreeVisitor);
		}
	}

}
